/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import java.io.*;
import java.net.*;
import UserInterfase.SimpleBoard;
import java.awt.Point;
import javax.swing.JOptionPane;


public class PlayNetGame extends Thread{

    private static BufferedReader mSocketReader = null;
    private static SimpleBoard simple_board = null;

    private static boolean isEnd = false;
    
    public static BufferedReader getMSocketReader() {
        return mSocketReader;
    }
    public static void setMSocketReader(BufferedReader mSocketReader) {
        PlayNetGame.mSocketReader = mSocketReader;
    }
    
    public static void setSimpleBoard(SimpleBoard simple_board) {
        PlayNetGame.simple_board = simple_board;
    }

    public void run()
    {
        while (!IvoCheckers.gameEnd)
        {
            if (isEnd)
            {
                try {
                    mSocketReader.close();
                    return;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                return;
            }
            String recvMove = null;
            try{
                recvMove = mSocketReader.readLine();
                int sourceX, sourceY, targetX, targetY;
                sourceX = convertCoordinate(Integer.parseInt(recvMove.substring(0, 1)));
                sourceY = convertCoordinate(Integer.parseInt(recvMove.substring(2, 3)));
                targetX = convertCoordinate(Integer.parseInt(recvMove.substring(4, 5)));
                targetY = convertCoordinate(Integer.parseInt(recvMove.substring(6, 7)));

                //simple_board.playerPressed(sourceX, sourceY);
                //simple_board.playerReleased(targetX, targetY);
                if (IvoCheckers.getGame().isLegalMove(sourceX, sourceY, targetX, targetY))
                {
                    boolean turn1 = IvoCheckers.getGame().isDarkPlayerTurn();
                    IvoCheckers.getGame().makeMove(sourceX, sourceY, targetX, targetY);
                    simple_board.spriteMove(new Point((sourceX - 1) * 50, (sourceY - 1) * 50) , 
                            new Point((targetX - 1) * 50, (targetY - 1) * 50));

                    boolean turn2 = IvoCheckers.getGame().isDarkPlayerTurn();
                    IvoCheckers.getFrontEnd().clearBoard();
                    IvoCheckers.getFrontEnd().placePieces();
                    if (IvoCheckers.getGame().gameHasBeenWon() == 1) {
                        JOptionPane.showMessageDialog(
                            null,
                            "You Lose The Game!",
                            "Game Over!",
                            JOptionPane.INFORMATION_MESSAGE);
                            IvoCheckers.endGame();
                    } else if (IvoCheckers.getGame().gameHasBeenWon() == 2) {
                            JOptionPane.showMessageDialog(
                                null,
                                "You Win The Game!",
                                "Game Over!",
                                JOptionPane.INFORMATION_MESSAGE);
                            IvoCheckers.endGame();
                    }
                    if (turn1 != turn2)
                        IvoCheckers.getGame().setBlockMove(false);
                }
                
            }
            catch (SocketException ex)
            {
                ex.printStackTrace();
                isEnd = true;
                JOptionPane.showMessageDialog(null, "Connection closed!");
                try{
                    mSocketReader.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                simple_board.setIsNetGame(false);
                IvoCheckers.gameEnd = true;
                return;
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
            catch (NumberFormatException ex)
            {
                ex.printStackTrace();
            }
            
        }
    }
    
    public void stopThread()
    {
        IvoCheckers.gameEnd = true;
        try {
        mSocketReader.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        while (!isEnd)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    private int convertCoordinate(int coordinate)
    {
        return 8 - coordinate + 1;
    }
}
